package com.taru.wxhsh.controller;

import com.taru.wxhsh.common.JsonResult;
import com.taru.wxhsh.constant.JResponseConstants;
import com.taru.wxhsh.pojo.Detail;
import com.taru.wxhsh.service.DetailService;
import com.taru.wxhsh.util.UidGenerators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**购物车
 * Created by HuKang on 2018/5/30.
 */

@Controller
public class DetailController {
    Logger logger = LoggerFactory.getLogger(DetailController.class);
    @Autowired
    DetailService detailService;

    /**
     * WX加入购物车
     *
     * @param detail
     * @return
     */
    @ResponseBody
    @RequestMapping("/detail/shopCartIn")
    public Object shopCartIn(Detail detail) {
        JsonResult<String> result = null;
        try {
            detail.setDetailUserId(UidGenerators.getUuid());//?
            detailService.insert(detail);
            result = new JsonResult<>(JResponseConstants.STATUS_OK, "加入购物车成功");
        } catch (Exception ex) {
            logger.error("加入购物车异常:{}", ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR, "加入购物车异常");
        }
        return result;
    }

    /**
     * 查询购物车列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/detail/qryCartList")
    public Object qryCartList(String uid) {
        JsonResult< List<Detail>> result = null;
        try {
            List<Detail> list = detailService.qryCartList(uid);
            result = new JsonResult< List<Detail>>(JResponseConstants.STATUS_OK, "查询购物车成功", list);
        } catch (Exception ex) {
            logger.error("查询购物车异常：{}", ex.getMessage());
            result = new JsonResult< List<Detail>>(JResponseConstants.STATUS_ERROR, "查询购物车异常");
        }
        return result;
    }

}